package one.chapter_1.unionfind;

import java.util.AbstractList;

public class QuickFindUF extends AbstractUF {
    private int []ids;
    public QuickFindUF(int count){
        super(count);
        ids=new int[count];
        for (int i=0;i<count;i++){
            ids[i]=i;
        }
    }
    @Override
    public void union(int p, int q) {
        int _p=find(p);
        int _q=find(q);
        if (_p ==_q){
            return;
        }
        for (int i=0;i<ids.length;i++){
            if (ids[i]==_q){
                ids[i]=_p;
            }
        }
        count--;
    }

    @Override
    public int find(int p) {
        return ids[p];
    }

    @Override
    public boolean connected(int p, int q) {
        return find(p)==find(q);
    }



}
